import { useUserStore } from '@/stores';
import axios, { type AxiosInstance } from 'axios';
const service: AxiosInstance = axios.create({
  baseURL: 'http://192.168.220.128:8080/admin', 
  timeout: 500000, // 请求超时时间
});

// axios 实例配置 
service.interceptors.request.use((config)  => {
  const userStore = useUserStore();
  const token = userStore.userInfo?.token; 
  
  // 白名单跳过 
  const whiteList = ['/login'];
  if (whiteList.some(path  => config.url?.includes(path)))  return config;
 
  // Token 注入 
  if (token) {
    config.headers.Authorization = `Bearer ${token}`;
  } else {
    window.location.href  = '/';
  }
  return config;
});
 
// 响应拦截器处理 401 过期 
service.interceptors.response.use( 
  (response) => response,
  (error) => {
    if (error.response?.status === 401)
      {
      useUserStore().clearUserInfo();
      window.location.href  = '/';
    }
    return Promise.reject(error); 
  }
);
export default service;